package com.xiaoxian.basic.service.impl;

import com.xiaoxian.basic.commoneumn.ErrorCode;
import com.xiaoxian.basic.constant.Constant;
import com.xiaoxian.basic.domain.dto.SmsCode;
import com.xiaoxian.basic.exception.CustomException;
import com.xiaoxian.basic.service.IVerifyCodeImgService;
import com.xiaoxian.basic.util.AjaxResult;
import com.xiaoxian.basic.util.SmsUtil;
import com.xiaoxian.basic.util.StrUtils;
import com.xiaoxian.basic.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
@Service
public class VerifyCodeImgServiceImpl implements IVerifyCodeImgService {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 图形验证码
     * @param imageCodeKey
     * @return
     */
    @Override
    public AjaxResult verifyCodeImg(String imageCodeKey) {
        //获取随机字符串
        String complexRandomString = StrUtils.getComplexRandomString(4);
        //获取图像验证码
        String s = VerifyCodeUtils.VerifyCode(115, 40, complexRandomString);
        //存入redis
        redisTemplate.opsForValue().set(imageCodeKey,complexRandomString,5, TimeUnit.MINUTES);
        //返回ajaxresult
        return AjaxResult.my().setPath(s);
    }

    /**
     * 手机验证码
     * @param smsCode
     * @return
     */
    @Override
    public AjaxResult sendSmsCode(SmsCode smsCode) {
        System.out.println(smsCode.getImageCodeKey());
        //获取缓存得验证码
        //- 参数校验
        //- 校验图形验证码是否正确
        String imageCode =(String) redisTemplate.opsForValue().get(smsCode.getImageCodeKey());
        //- congredis中获取图形验证码
        //- 没有（业务异常）
        if(imageCode==null){
            throw new CustomException(ErrorCode.SMS_CODE_ERROR.getCode());
        }
        //- 有

        //- 通过前端传递的uuid从redis中获取，和传递过来的图形验证码进行比对（忽略大小写）
        if(!imageCode.equalsIgnoreCase(smsCode.getImageCode())){
            throw new CustomException(ErrorCode.SMS_CODE_DIF.getCode());
        }
        //- 通过手机号拼接业务键去redis中查询
        String smsKey= smsCode.getPhone()+ Constant.REGISTER_KEY;
        String smsValue = (String)redisTemplate.opsForValue().get(smsKey);

        String smsCodeStr;
        //已过期或者第一次来获取验证码
        if(smsValue==null){
            //- 没有
            //生产随机验证码
            smsCodeStr= StrUtils.getComplexRandomString(4);

            //发送短信
//            try {
//                SmsUtil.sendSms("13568182924","验证码为："+smsCodeStr);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
            System.out.println("-----------------"+smsCodeStr);

        }else {
            //重新发送
            Long sendTime =Long.valueOf(smsValue.split(":")[1]);
            //判断是否超过一分钟
            if(!((System.currentTimeMillis()-sendTime)>=1*60*1000)){
                throw new CustomException(ErrorCode.SMS_NO_TIMEOUT.getCode());
            }
            //发送老的验证码
            smsCodeStr = smsValue.split(":")[0];
            //发送短信
            //发送短信
//            try {
//                SmsUtil.sendSms("13568182924","验证码为："+smsCodeStr);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
            System.out.println("-----------------"+smsCodeStr);
        }
        //保存到redis
        redisTemplate.opsForValue().set(smsKey,smsCodeStr+":"+System.currentTimeMillis(),3,TimeUnit.MINUTES);

        return AjaxResult.my();
    }
}
